/**
 * @link https://leetcode.cn/problems/palindrome-number/?envType=study-plan-v2&envId=top-interview-150
 */
function isPalindrome(x: number): boolean {
  if (x < 0) return false;
  let str = x.toString();
  let i = 0;
  let j = str.length - 1;
  while (i < j) {
    // console.log(i, j, str[i], str[j]);
    if (str[i] !== str[j]) return false;
    i += 1;
    j -= 1;
  }

  return true;
}

/**
 * @link https://leetcode.cn/problems/plus-one/?envType=study-plan-v2&envId=top-interview-150
 */
function plusOne(digits: number[]): number[] {
  let cmp = 1;
  for (let i = digits.length - 1; i >= 0; i--) {
    if (cmp === 1) {
      if (digits[i] + cmp >= 10) {
        digits[i] = 0;
        cmp = 1;
      } else {
        digits[i] = digits[i] + 1;
        cmp = 0;
      }
    } else {
      break;
    }
  }

  if (cmp === 1) {
    digits.unshift(1);
  }

  return digits;
}

/**
 * @link https://leetcode.cn/problems/factorial-trailing-zeroes/?envType=study-plan-v2&envId=top-interview-150
 */
function trailingZeroes(n: number): number {
  if (!n) return 0;
  let is2 = 0;
  let is5 = 0;
  for (let i = 1; i <= n; i++) {
    let tmp = i;
    while (tmp % 2 === 0) {
      tmp /= 2;
      is2 += 1;
    }
    while (tmp % 5 === 0) {
      tmp /= 5;
      is5 += 1;
    }
  }
  console.log(is2, is5);

  return Math.min(is2, is5);
}

/**
 * @link https://leetcode.cn/problems/sqrtx/?envType=study-plan-v2&envId=top-interview-150
 */
function mySqrt(x: number): number {
  console.log(Math.sqrt(x));

  return Math.floor(Math.sqrt(x));
}

/**
 * @link https://leetcode.cn/problems/powx-n/?envType=study-plan-v2&envId=top-interview-150
 */
function myPow(x: number, n: number): number {
  return Math.pow(x, n);
}

/**
 * @link https://leetcode.cn/problems/max-points-on-a-line/?envType=study-plan-v2&envId=top-interview-150
 */
function maxPoints(points: number[][]): number {
  return 0;
}

export default { isPalindrome, plusOne, trailingZeroes, mySqrt };
